<?php
class VideosController extends AppController {

	var $name = 'Videos';
	var $uses = array('Video','Copyright');
	
	// Default Settings
	var $directory = 'files/Video/';
	var $allowedTypes = array('video/mpeg');
	
	var $paginate = array('fields'=>array('Video.id', 'Video.filename', 'Video.filesize', 'Video.mimetype','Video.copyright_owner','Video.copyright_id'),'limit'=>10, 'order'=>array('Video.created'=>'DESC'));


	function index() {
		$this->Video->recursive = 0;
		$this->set('videos', $this->paginate());
	}

	function view($id = null) {
		if(!$id) {
			$this->Session->setFlash('Invalid Video.');
			$this->redirect(array('action'=>'index'), null, true);
		}
		$this->set('video', $this->Video->read(null, $id));
	}

	function add() {
		if(!empty($this->data)) {
			$this->cleanUpFields();
			$this->Video->create();
			if($this->Video->save($this->data)) {
				$this->Session->setFlash('The Video has been saved');
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				$this->Session->setFlash('The Video could not be saved. Please, try again.');
			}
		}
		$copyrights = $this->Video->Copyright->generateList();
		$this->set(compact('copyrights'));
	}

	function edit($id = null) {
		if(!$id && empty($this->data)) {
			$this->Session->setFlash('Invalid Video');
			$this->redirect(array('action'=>'index'), null, true);
		}
		if(!empty($this->data)) {
			$this->cleanUpFields();
			if($this->Video->save($this->data)) {
				$this->Session->setFlash('The Video saved');
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				$this->Session->setFlash('The Video could not be saved. Please, try again.');
			}
		}
		if(empty($this->data)) {
			$this->data = $this->Video->read(null, $id);
		}
		$copyrights = $this->Video->Copyright->generateList();
		$this->set(compact('copyrights'));
	}

	function delete($id = null) {
		if(!$id) {
			$this->Session->setFlash('Invalid id for Video');
			$this->redirect(array('action'=>'index'), null, true);
		}
		if($this->Video->del($id)) {
			$this->Session->setFlash('Video #'.$id.' deleted');
			$this->redirect(array('action'=>'index'), null, true);
		}
	}


	function admin_index() {
		$this->Video->recursive = 0;
		$this->set('videos', $this->paginate());
	}

	function admin_view($id = null) {
		if(!$id) {
			$this->Session->setFlash('Invalid Video.');
			$this->redirect(array('action'=>'index'), null, true);
		}
		$this->set('video', $this->Video->read(null, $id));
	}

	function admin_add()
	{
		uses('folder');		
		
		$folder = new Folder($this->directory, false, 0755);
		$upload = array();
		
		$copyright_type = $this->Copyright->generateList();
		$this->set(compact('copyright_type'));
		
		if(!empty($this->data) && $this->data['Video']['filename']['error'] == 0)
		{
			// Explode file name and extention
			$myfile = $this->FileOps->explodeFilename($this->data['Video']['filename']['name']);
			
			// Lets get the metadata first
			$upload['mimetype'] = $this->data['Video']['filename']['type'];			
			$upload['filesize'] = $this->data['Video']['filename']['size'];
			$upload['filename'] = $myfile['filename'];
			$upload['copyright_id'] = $this->data['Video']['copyright_id'];
			$upload['copyright_owner'] = $this->data['Video']['copyright_owner'];
			
			// The temporary file we need to copy once uploaded.
			$upload['tmp'] = $this->data['Video']['filename']['tmp_name'];
			if (!file_exists($this->directory . $upload['filename'] . '.' . $myfile['ext']))
			{
				if (!($moved = $this->FileOps->movefile($this->directory, $upload['tmp'], $upload['filename'] . '.' . $myfile['ext'])))
				{
                	$this->Session->setFlash('File system save failed.');
					return false;
            	} else {
            		$this->cleanUpFields();
					$this->Video->create();
					if($this->Video->save($upload))
					{
						$this->Ffmpeg->convert($this->directory . $upload['filename'] . '.' . $myfile['ext'],"files/File/",$upload['filename'], $this->Conf->get('video.width'), $this->Conf->get('video.height'), $this->Conf->get('video.samplerate'), $this->Conf->get('video.bitrate'));
						$this->Session->setFlash('The Video has been saved');	
						$this->redirect(array('action'=>'index'), null, true);
					} else {
						$this->Session->setFlash('The Video could not be saved. Please, try again.');
					}
				}
			}
		}
	}

	function admin_edit($id = null) {
		if(!$id && empty($this->data)) {
			$this->Session->setFlash('Invalid Video');
			$this->redirect(array('action'=>'index'), null, true);
		}
		if(!empty($this->data)) {
			$this->cleanUpFields();
			if($this->Video->save($this->data)) {
				$this->Session->setFlash('The Video saved');
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				$this->Session->setFlash('The Video could not be saved. Please, try again.');
			}
		}
		if(empty($this->data)) {
			$this->data = $this->Video->read(null, $id);
		}
		$copyrights = $this->Video->Copyright->generateList();
		$this->set(compact('copyrights'));
	}

	function admin_delete($id = null) {
		if(!$id) {
			$this->Session->setFlash('Invalid id for Video');
			$this->redirect(array('action'=>'index'), null, true);
		}
		if($this->Video->del($id)) {
			$this->Session->setFlash('Video #'.$id.' deleted');
			$this->redirect(array('action'=>'index'), null, true);
		}
	}
}
?>